using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class BiochipView : MonoBehaviour
{
	public BiochipManager biochipManager;
	public List<GameObject> Buttons;
	public List<GameObject> Lights;
	public GameObject popUpTryAgain;
	public GameObject btnAgain;
	public GameObject btnGiveUp;

	// Use this for initialization
	void Awake ()
	{
		biochipManager = GameObject.Find("Biochip").GetComponent<BiochipManager>();
		Buttons = new List<GameObject>(GameObject.FindGameObjectsWithTag("Buttons").OrderBy(x => x.name).ToList());
		Lights = new List<GameObject>(GameObject.FindGameObjectsWithTag("Lights").OrderBy(x => x.name).ToList());
		Lights.OrderBy(x => x.name).ToList();
		popUpTryAgain = GameObject.Find("Biochip_Failed");
		btnAgain = GameObject.Find("Again");
		btnGiveUp = GameObject.Find("GiveUp");

		popUpTryAgain.SetActive(false);
	}
	
	// Update is called once per frame
	void Update ()
	{
		for(int i = 0; i < biochipManager.biochipData.lightButtons.Count; i++)
			if(biochipManager.biochipData.lightButtons[i] == true)
				Lights[i].SetActive(false); // or whatever what activates lights
			else
				Lights[i].SetActive(true);

		if(Input.GetMouseButtonDown(0))
			checkForCollision();
	}

	void checkForCollision()
	{
		for(int i = 0; i < Buttons.Count; i++)
			if(biochipManager.biochipData.mouse.collider2D.bounds.Intersects(Buttons[i].collider2D.bounds))
				biochipManager.onButtonPress(i);

		if(biochipManager.biochipData.mouse.collider2D.bounds.Intersects(btnAgain.collider2D.bounds))
			biochipManager.onButtonPress(btnAgain.name);

		if(biochipManager.biochipData.mouse.collider2D.bounds.Intersects(btnGiveUp.collider2D.bounds))
			biochipManager.onButtonPress(btnGiveUp.name);
	}

	public void activatePopUp()
	{
		popUpTryAgain.SetActive(!popUpTryAgain.activeSelf);
	}
}

